package com.android.gilastv.ui.view;

import static android.content.Context.BATTERY_SERVICE;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.util.DateUtil;
import com.android.baselibrary.util.NetUtils;
import com.android.baselibrary.view.Comm_EditView;
import com.android.gilastv.R;
import com.android.gilastv.ui.activity.SearchActivity;

/**
 * 标题控件
 */
public class Comm_HeadStateView extends LinearLayout {
    public Comm_HeadStateView(Context context) {
        super(context);
        this.context = context;
        initView(null);
    }

    public Comm_HeadStateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }

    public Comm_HeadStateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(attrs);
    }

    Context context;
    public ImageView public_title_left_img, iv_net, iv_wifi;
    TextView tv_time,tv_content;
    ProgressBar pb_percent;
    BatteryManager batteryManager;
    Handler handler = new Handler();
    CommCallBack callBackInput;
    public Comm_EditView et_content;

    private void initView(AttributeSet attrs) {
        View.inflate(context, R.layout.view_comm_head_stateview, this);
        public_title_left_img = findViewById(R.id.public_title_left_img);
        tv_time = findViewById(R.id.tv_time);
        iv_net = findViewById(R.id.iv_net);
        iv_wifi = findViewById(R.id.iv_wifi);
        pb_percent = findViewById(R.id.pb_percent);
        et_content = findViewById(R.id.et_content);
        tv_content= findViewById(R.id.tv_content);

        batteryManager = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
        handler.post(runnable);

        tv_content.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, SearchActivity.class));
            }
        });
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            tv_time.setText(DateUtil.getDateFromTimeLine(System.currentTimeMillis(), "HH:mm"));
            pb_percent.setProgress(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY));
            if (NetUtils.isWifiConnected(context)) {
                iv_wifi.setVisibility(VISIBLE);
            } else {
                iv_wifi.setVisibility(GONE);
            }
            if (TextUtils.isEmpty(NetUtils.getOperator(context))) {
                iv_net.setVisibility(GONE);
            } else {
                iv_net.setVisibility(VISIBLE);
            }

            handler.postDelayed(runnable, 1000 * 2);
        }
    };

    public void setInputCallback(CommCallBack callBackInput) {
        this.callBackInput = callBackInput;
        if (callBackInput != null) {
            tv_content.setVisibility(GONE);
            et_content.setVisibility(VISIBLE);
            et_content.setInputCallBack(callBackInput);
        }
    }

    public void setLeftClickFinish(Activity activity) {
        public_title_left_img.setVisibility(VISIBLE);
        public_title_left_img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }
}
